home *** CD-ROM | disk | FTP | other *** search
/ Your Choice 3 / Your Choice Software Collection 3.iso / dos / secdr13d / cryptdsk.c < prev    next >
C/C++ Source or Header  |  1994-04-21  |  17KB  |  569 lines

  1. /* Secure Drive CRYPTDSK V1.3d */
  2. /* Encrypts/decrypts disks */
  3.  
  4. #include "secdrv.h"
  5.  
  6. extern char pass1[MAXPASS];
  7. extern char pass2[MAXPASS];
  8. extern char Old13Check[4];
  9.  
  10. extern int  tsr_not_installed;
  11. extern int  tsr_wrong_version;
  12. extern int  df10,ef10;
  13. extern int  incfo, outcfo;  /*1.3c*/
  14. extern char compat_mode;
  15.  
  16. void readseca(unsigned drive,unsigned head,unsigned cyl,unsigned sector,
  17.      unsigned nsects,unsigned secsize,unsigned char *buffer);
  18. void writeseca(unsigned drive,unsigned head,unsigned cyl,unsigned sector,
  19.      unsigned nsects,unsigned secsize,unsigned char *buffer);
  20. void freebuf(void);
  21. void clrbufs(void);
  22.  
  23. unsigned char *buf;
  24. char *pgpv;
  25.  
  26. int main()
  27. {
  28. unsigned drive,firstcyl,firsthead,encrypt,decrypt;            /*1.1*/
  29. unsigned cyl,head,sector,maxcyl,maxhead,maxsector,secsize,t;
  30. unsigned serial[2];
  31. unsigned char dkey[16],dcheck[4],iv[8],tiv[8];                /*1.1*/
  32. unsigned char ekey[16],echeck[4];                             /*1.1*/
  33. unsigned char *bufptr,*dummy;
  34. unsigned i,hdx;
  35. int sethdsafe=0;
  36. int dkeyexp=0;
  37. int ekeyexp=0;
  38.  
  39. char drvltr,r;                                                /*1.1*/
  40. word16 dexpkey[52],eexpkey[52];                               /*1.1*/
  41.  
  42. clrscr();
  43.  
  44.  
  45. if(!(buf=malloc(512))) {
  46.     printf("Error: unable to allocate memory for sector buffer.\n");
  47.     exit(1); }
  48.  
  49. atexit(freebuf);
  50.  
  51. printf("\
  52. Secure Drive CryptDisk Version 1.3d\n\
  53. \n\
  54. This program will encrypt or decrypt a floppy disk or hard drive\
  55.  partition.\n");
  56.  
  57. pgpv=getenv("PGPPASS");
  58. set_compat_mode();
  59.  
  60. cryptdata=gettsradr();
  61.  
  62. if(tsr_wrong_version)
  63.  {
  64.   printf ("\nERROR: Wrong Version (not 1.3D) of SECTSR is Installed\n");
  65.   exit(1);
  66.  }
  67.  
  68. if (tsr_not_installed)
  69.   printf("Warning: SECTSR not installed.\n");
  70.  
  71. askdrive:
  72. printf("\nEnter the letter of the drive to process, or X to enter the\n\
  73. drive, cylinder, and head manually, or Z to cancel: ");
  74. while(!isalpha(drvltr=toupper(getch())));
  75. printf("%c\n",drvltr);
  76.  
  77.  
  78. if(drvltr=='A') { firstcyl=0; firsthead=0; drive=0; }
  79. else if(drvltr=='B') { firstcyl=0; firsthead=0; drive=1; }
  80.  
  81. else if(drvltr=='X') {
  82.     printf("\nEnter physical drive (0,1,2,...), cylinder, and head for the\
  83.  beginning\n\(boot sector) of this partition: ");
  84.     scanf("%u,%u,%u",&drive,&firstcyl,&firsthead);
  85.     drive+=0x80;
  86.     }
  87.  
  88. else if(drvltr=='Z') { printf("\n"); exit(0); }
  89.  
  90. else {
  91.     drive=255;
  92.     readptbl(drvltr,&drive,&firsthead,&firstcyl);  /*1.1*/
  93.     if(drive==255) {
  94.         printf("\nDrive not found.\n\n");
  95.         goto askdrive; }
  96.     printf("\nDrive %c is physical hard drive %u, cylinder %u, head %u\n\n",
  97.             drvltr,drive,firstcyl,firsthead);     /*1.3b*/
  98.     drive+=0x80;
  99.     }
  100.  
  101. if(drive<0x80) {
  102.    printf("\nInsert disk in drive %c and press any key to\
  103.  continue ",drvltr);
  104.    getch();
  105.    printf("\n\n"); }
  106.  
  107. readsec(drive,firsthead,firstcyl,1,1,buf);
  108. if((buf[510]!=0x55)||(buf[511]!=0xaa)) {
  109.     printf("This is not a boot sector.\n\n");
  110.     exit(1); }
  111.  
  112.  
  113. decrypt=(!memcmp(buf+incfo,"CRYP",4) || !memcmp(buf+outcfo,"CRYP",4));
  114. encrypt=!decrypt;                       /*1.1*/
  115.  
  116. calcdiskparams(buf,&maxcyl,&maxhead,&maxsector,
  117.                &secsize,serial);
  118.  
  119. printf("This disk has \
  120. %u cylinders, %u sectors, %u heads, sector size %u bytes\n\n",
  121.        maxcyl+1,maxsector,maxhead,secsize);
  122.  
  123. if((buf=realloc(buf,maxsector*secsize)) != NULL)
  124.     printf("Allocated %u bytes for track buffer\n\n",
  125.             maxsector*secsize);
  126. else {
  127.     printf("Error: unable to allocate %u bytes for track buffer\n",
  128.             maxsector*secsize);
  129.     exit(1); }
  130.  
  131. if(encrypt)
  132.     {
  133.     int needkey=1;
  134.  
  135.  
  136.     printf("This disk is not encrypted. Do you want to encrypt it? ");
  137.     if(!getyn())
  138.         {
  139.         printf("\n");
  140.         exit(0);
  141.         }
  142.  
  143.  
  144.     if (pgpv != NULL)
  145.      {
  146.       printf("\nUse PGPPASS as passphrase? ");
  147.       if (getyn())
  148.        {
  149.         strcpy(pass1,pgpv);
  150.         setkeye(ekey,echeck);
  151.         printf("\nPGPPASS entered as %s passphrase\n",
  152.                ef10 ? "(V 1.0)":"(V 1.1)");       /*1.3b*/
  153.         clrbufs();
  154.         needkey=0;
  155.        }
  156.      }
  157.  
  158.     if (needkey && cryptdata != NULL && drive<0x80)
  159.      {
  160.       if (memcmp(cryptdata->fkeychk,"\xff\xff\xff\xff",4) &&
  161.          (compat_mode != 'N') == (cryptdata->fkeyv10 != 0))
  162.          /*key taken from SECTSR for encryption must match SD10CMP*/
  163.        {
  164.         printf("Use Floppy Key already in SECTSR? ");
  165.         if (getyn())
  166.          {
  167.           memcpy(echeck,cryptdata->fkeychk,4);
  168.           memcpy(eexpkey,cryptdata->fkey,104);
  169.           ef10=cryptdata->fkeyv10;                   /*1.3b*/
  170.           ekeyexp=1;
  171.           needkey=0;
  172.          }
  173.        }
  174.      }
  175.  
  176.     if (needkey && cryptdata != NULL && drive<0x80)
  177.      {
  178.       if (memcmp(cryptdata->hkeychk,"\xff\xff\xff\xff",4) &&
  179.          (compat_mode != 'N') == (cryptdata->hkeyv10 != 0))
  180.          /*key taken from SECTSR for encryption must match SD10CMP*/
  181.        {
  182.         printf("Use Hard Disk key already in SECTSR? ");
  183.         if (getyn())
  184.          {
  185.           memcpy(echeck,cryptdata->hkeychk,4);
  186.           memcpy(eexpkey,cryptdata->hkey,104);
  187.           ef10=cryptdata->hkeyv10;                   /*1.3b*/
  188.           ekeyexp=1;
  189.           needkey=0;
  190.          }
  191.        }
  192.      }
  193.  
  194.     if (needkey)
  195.      {
  196.       getkey(ekey,echeck,TRUE);
  197.       clrbufs();
  198.      }
  199.     }
  200. else
  201.     {
  202.      int needdkey=1;
  203.      printf("This disk is encrypted.\n\
  204. Select (C)hange passphrase, (D)ecrypt, (E)xit: ");           /*1.1*/
  205.      do r=toupper(getch()); while(r!='C'&&r!='D'&&r!='E');   /*1.1*/
  206.      printf("%c\n",r);                                       /*1.1*/
  207.      if(r=='E')                                              /*1.1*/
  208.       {
  209.        printf("\n");
  210.        exit(0);
  211.       }
  212.  
  213.      if (cryptdata != NULL && drive<0x80 &&
  214.          (!memcmp(cryptdata->fkeychk,buf+incfo+4,4)  ||
  215.           !memcmp(cryptdata->fkeychk,buf+outcfo+4,4)  )  )
  216.       {
  217.        printf("Floppy Key already in SECTSR will decrypt this diskette.\n");
  218.        memcpy(dcheck,cryptdata->fkeychk,sizeof(dcheck));
  219.        memcpy(dexpkey,cryptdata->fkey,104);
  220.        df10=cryptdata->fkeyv10;
  221.        needdkey=0;
  222.        dkeyexp=1;
  223.       }
  224.  
  225.      if (needdkey && pgpv != NULL)
  226.       {
  227.        strcpy(pass1,pgpv);
  228.        setkeydf(dkey,dcheck,buf);
  229.  
  230.        if(!df10 && !memcmp(Old13Check,buf+outcfo+4,4))
  231.         {
  232.          printf("Check bytes in Disk %c: Boot Sector need updating from 1.3 to 1.1/1.3A. Proceed? ",
  233.                 drvltr);
  234.          if(getyn())
  235.           {
  236.            memcpy(buf+outcfo+4,dcheck,4);
  237.            writesec(drive,firsthead,firstcyl,1,1,buf);
  238.           }
  239.         }
  240.  
  241.        if(memcmp(dcheck,buf+incfo+4,4) && memcmp(dcheck,buf+outcfo+4,4))
  242.         {
  243.          printf("\nPGPPASS is wrong passphrase.\n");
  244.         }
  245.        else
  246.         {
  247.          printf("\nPGPPASS entered as ");
  248.          if(r=='C') printf("old ");                 /*1.1*/
  249.          if(df10)
  250.           printf("(V 1.0) ");
  251.          else
  252.           printf("(V 1.1) ");
  253.          printf("passphrase will decrypt this disk.\n");
  254.          needdkey=0;
  255.         }
  256.       }
  257.  
  258.  
  259.      if (needdkey)
  260.       {
  261.        for(t=0;t<3;t++)
  262.         {
  263.          printf("\nEnter ");                        /*1.1*/
  264.          if(r=='C') printf("old ");                 /*1.1*/
  265.          printf("passphrase: ");                    /*1.1*/
  266.          getkeydf(dkey,dcheck,buf);                 /*1.3b*/
  267.          if(!df10 && !memcmp(Old13Check,buf+outcfo+4,4))
  268.           {
  269.            printf("Check bytes in Disk %c: Boot Sector need updating from 1.3 to 1.1/1.3A. Proceed? ",
  270.                   drvltr);
  271.            if(getyn())
  272.             {
  273.              memcpy(buf+outcfo+4,dcheck,4);
  274.              writesec(drive,firsthead,firstcyl,1,1,buf);
  275.             }
  276.           }
  277.          clrbufs();
  278.          if(!memcmp(dcheck,buf+incfo+4,4) || !memcmp(dcheck,buf+outcfo+4,4))
  279.           break;                                    /*1.3c*/
  280.          printf("Wrong passphrase.\n");
  281.          if(t==2) exit(1);                          /*1.1*/
  282.         }
  283.       }
  284.     }
  285.     if(r=='C')                                     /*1.1*/
  286.      {                                             /*1.1*/
  287.       int needekey=1;
  288.       encrypt=TRUE;                                /*1.1*/
  289.       if (pgpv != NULL)
  290.        {
  291.         printf("\nUse PGPPASS as new passphrase? ");
  292.         if (getyn())
  293.          {
  294.           strcpy(pass1,pgpv);                                   /*1.3b*/
  295.           setkeye(ekey,echeck);                                 /*1.3b*/
  296.           printf("\nPGPPASS entered as new %s passphrase\n",
  297.                   ef10 ? "(V 1.0)":"(V 1.1)");                  /*1.3b*/
  298.           needekey=0;
  299.          }
  300.        }
  301.  
  302.         if (needekey && cryptdata != NULL && drive<0x80 )
  303.          {
  304.           if (!dkeyexp) en_key_idea((word16 *)dkey,dexpkey);
  305.           if (memcmp(dexpkey,cryptdata->fkey,sizeof(dexpkey)) != 0)
  306.             /*floppy key can be new key if not also old key!*/
  307.            {
  308.             if (memcmp(cryptdata->fkeychk,"\xff\xff\xff\xff",4))
  309.              {
  310.               printf("Use Floppy Key already in SECTSR as new key? ");
  311.               if (getyn())
  312.                {
  313.                 memcpy(echeck,cryptdata->fkeychk,sizeof(echeck));
  314.                 memcpy(eexpkey,cryptdata->fkey,104);
  315.                 needekey=0;
  316.                 ekeyexp=1;
  317.                 ef10=cryptdata->fkeyv10;
  318.                }
  319.              }
  320.            }
  321.          }
  322.  
  323.         if (needekey && cryptdata != NULL && drive<0x80 )
  324.          {
  325.           if (!dkeyexp) en_key_idea((word16 *)dkey,dexpkey);
  326.           if (memcmp(dexpkey,cryptdata->hkey,sizeof(dexpkey)) != 0)
  327.             /*HD key can be new key if not also old key!*/
  328.            {
  329.             if (memcmp(cryptdata->hkeychk,"\xff\xff\xff\xff",4))
  330.              {
  331.               printf("Use Hard Drive Key already in SECTSR as new key? ");
  332.               if (getyn())
  333.                {
  334.                 memcpy(echeck,cryptdata->hkeychk,sizeof(echeck));
  335.                 memcpy(eexpkey,cryptdata->hkey,104);
  336.                 needekey=0;
  337.                 ekeyexp=1;
  338.                 ef10=cryptdata->hkeyv10;
  339.                }
  340.              }
  341.            }
  342.          }
  343.  
  344.         if (needekey)
  345.          {
  346.           printf("\n");                              /*1.1*/
  347.           getkey(ekey,echeck,TRUE);                  /*1.1*/
  348.           clrbufs();
  349.          }
  350.  
  351.       if (memcmp(dkey,ekey,sizeof(dkey)) == 0)
  352.        {
  353.         printf("ERROR: Attempt to (C)hange Passphrase with same Key\n");
  354.         exit(1);
  355.        }
  356.      }
  357.  
  358.    if(cryptdata != NULL  && drive >= 0x80)
  359.     {
  360.      /*find empty/Duplicate HD slot */
  361.      for (i=0;i<MAXDRV;i++)
  362.       {
  363.        if (  cryptdata->hd[i].dddrv    == 0  ||
  364.             (cryptdata->hd[i].dddrv    == drive &&
  365.              cryptdata->hd[i].firstcyl == firstcyl) )
  366.         {
  367.          printf("SECTSR will protect drive during %s.\n",
  368.           decrypt ?
  369.             (encrypt ? "passphrase change" : "decryption")
  370.            : "encryption");
  371.          sethdsafe=1;
  372.          break;
  373.         }
  374.       }
  375.     }
  376.  
  377. printf("\nLast chance to abort. Continue? ");
  378. if(!getyn()) exit(1);
  379. bdos(0x0D, 0, 0);          /* Reset Disk Subsystem - Flush all buffers */
  380. clrscr();
  381. gotoxy(1,8);
  382. if (sethdsafe)
  383.  {
  384.   hdx=i;
  385.   if (cryptdata->hd[i].dddrv == 0)
  386.    {
  387.     cryptdata->hd[i].dddrv=drive;
  388.     cryptdata->hd[i].drvltr=drvltr;
  389.     cryptdata->hd[i].firstcyl=firstcyl;
  390.     cryptdata->hd[i].firsthd=firsthead;
  391.     cryptdata->hd[i].firstsec=1;
  392.     cryptdata->hd[i].lastcyl=firstcyl+maxcyl;
  393.     cryptdata->hd[i].maxsec=maxsector;
  394.     cryptdata->hd[i].maxhd=maxhead;
  395.     cryptdata->hd[i].secsize=secsize;
  396.     cryptdata->hd[i].active=0;
  397.    }
  398.   else if (cryptdata->hd[i].active)
  399.    {
  400.     cryptdata->hd[i].active=0;
  401.     memset(cryptdata->hkey,0xbb,104);
  402.    }
  403.  }
  404.  
  405. if (encrypt && decrypt)
  406.  printf("Changing Old %s Passphrase to New %s Passphrase\n",
  407.          df10 ? "(V 1.0) " : "(V 1.1) ",
  408.          ef10 ? "(V 1.0) " : "(V 1.1) ");
  409. else if (encrypt)
  410.  printf("Encrypting %s",
  411.          ef10 ? "(V 1.0) ":"(V 1.1) ");
  412. else
  413.  printf("Decrypting %s",
  414.          df10 ? "(V 1.0) " : "(V 1.1) ");
  415. printf("disk %c:  %u cyls, %u sectors, %u heads, sector size %u bytes",
  416.        drvltr,maxcyl+1,maxsector,maxhead,secsize);
  417.  
  418. if(encrypt && !ekeyexp) en_key_idea((word16 *)ekey,eexpkey);     /*1.1,1.3*/
  419. if(decrypt && !dkeyexp) en_key_idea((word16 *)dkey,dexpkey);     /*1.1,1.3*/
  420.  
  421. printf("\n");
  422. for(cyl=0;cyl<=maxcyl;cyl++)
  423.  for(head=0;head<maxhead;head++)
  424.   {
  425.         if(cyl==0&&head<firsthead) head=firsthead;
  426.         gotoxy(1,11);
  427.         printf("Cylinder %u, Head %u ",cyl,head);     /*1.1*/
  428.         readseca(drive,head,cyl+firstcyl,1,maxsector,secsize,buf);
  429.         bufptr=buf;
  430.         for(sector=1;sector<=maxsector;sector++)
  431.          {
  432.           if(cyl==0&&head==firsthead&§or==1)
  433.            if(encrypt)
  434.             {
  435.              memcpy(&buf[outcfo],"CRYP",4);      /*1.3c*/
  436.              memcpy(&buf[outcfo+4],echeck,4);    /*1.3c*/
  437.              if (!memcmp(buf+3,"CRYP",4))
  438.               memcpy(buf+3,"MSDOS   ",8);
  439.             }
  440.            else
  441.             {
  442.              if (!memcmp(buf+3,"CRYP",4))
  443.               memcpy(buf+3,"MSDOS   ",8);
  444.              memcpy(buf+outcfo,"MSDOS   ",8);
  445.             }
  446.            else
  447.             {
  448.              t=cyl+firstcyl;
  449.              iv[0]=t%256;
  450.              iv[1]=t/256;
  451.              iv[2]=head;
  452.              iv[3]=sector;
  453.              iv[4]=serial[0]%256;
  454.              iv[5]=serial[0]/256;
  455.              iv[6]=serial[1]%256;
  456.              iv[7]=serial[1]/256;
  457.              if(decrypt)                                 /*1.1*/
  458.               {
  459.                memcpy(tiv,iv,sizeof(iv));
  460.                IdeaCFB(tiv,dexpkey,dummy,dummy,1);
  461.                IdeaCFBx(tiv,dexpkey,bufptr,bufptr,secsize/8+1);
  462.               }
  463.              if(encrypt)                                 /*1.1*/
  464.               {
  465.                memcpy(tiv,iv,sizeof(iv));
  466.                IdeaCFB(tiv,eexpkey,dummy,dummy,1);
  467.                IdeaCFB(tiv,eexpkey,bufptr,bufptr,secsize/8+1);
  468.               }
  469.             }
  470.            bufptr+=secsize;
  471.          }
  472.         writeseca(drive,head,cyl+firstcyl,1,maxsector,secsize,buf);
  473.        }
  474.  
  475.  
  476. for(t=0;t<16;t++) { ekey[t]='\0'; dkey[t]='\0'; }   /*1.1*/
  477. for(t=0;t<52;t++) { eexpkey[t]=0; dexpkey[t]=0; }   /*1.1*/
  478.  
  479. gotoxy(1,12);
  480. printf("\n\nDone.\n");
  481. bdos(0x0D, 0, 0);          /* Reset Disk Subsystem - Flush all buffers */
  482. if (sethdsafe)
  483.  {
  484.   if (encrypt)
  485.    {
  486.     printf("Encrypted hard disk now in safe mode.  Use LOGIN to access.\n");
  487.     if (!decrypt)
  488.      printf("Before re-boot, add appropriate LOGIN /S to AUTOEXEC.BAT.\n");
  489.    }
  490.   else
  491.    {
  492.     cryptdata->hd[hdx].dddrv = 0x07f;
  493.      /*remove decrypted hard disk from disk table.
  494.        note: re-boot required to recover drive table slot*/
  495.     printf("Decrypted disk may now be accessed. Before re-boot,\n\
  496. remove corresponding LOGIN  /S from AUTOEXEC.BAT.\n");
  497.    }
  498.  }
  499. return(0);
  500. }
  501.  
  502. void freebuf(void)
  503. {
  504. free(buf);
  505. }
  506.  
  507. void readseca(unsigned drive,unsigned head,unsigned cyl,unsigned sector,
  508.      unsigned nsects,unsigned secsize,unsigned char *buffer)
  509. {
  510. unsigned i,j;
  511. char c;
  512. unsigned r;
  513. for(i=0;i<3;i++)
  514.  {
  515.   r=rldbios(2,drive,head,cyl,sector,nsects,buffer);
  516.   if (!r) return;
  517.  }
  518. gotoxy(1,13);
  519. printf("\nRead error (%02X): drive %02x, head %u, cyl %u\n",
  520.        r,drive,head,cyl);
  521. printf("Reading one sector at a time.\n");
  522. for(j=0;j<nsects;j++) {
  523.   for(i=0;i<3;i++)
  524.    {
  525.     r=rldbios(2,drive,head,cyl,sector+j,1,buffer);
  526.     if(!r) goto goodsec;
  527.    }
  528.   gotoxy(1,15);
  529.   printf("Bad sector (%02X): drive %02x, head %u, cyl %u, sector %u\n",
  530.          r,drive,head,cyl,sector+j);
  531.   goodsec:
  532.   buffer+=secsize;
  533.   }
  534. }
  535.  
  536. void writeseca(unsigned drive,unsigned head,unsigned cyl,unsigned sector,
  537.      unsigned nsects,unsigned secsize,unsigned char *buffer)
  538. {
  539. unsigned i,j;
  540. char c;
  541. unsigned r;
  542. for(i=0;i<3;i++)
  543.  {
  544.   r=rldbios(3,drive,head,cyl,sector,nsects,buffer);
  545.   if(!r) return;
  546.  }
  547. gotoxy(1,13);
  548. if (r == 03 && drive<0x80)
  549.  {
  550.   printf("\nERROR: Disk %c is Write-Protected!\n",drive+'A');
  551.   exit(1);
  552.  }
  553. printf("\nWrite error (%02X): drive %02x, head %u, cyl %u\n",
  554.        r,drive,head,cyl);
  555. printf("Writing one sector at a time.\n");
  556. for(j=0;j<nsects;j++) {
  557.   for(i=0;i<3;i++)
  558.    {
  559.     r=rldbios(3,drive,head,cyl,sector+j,1,buffer);
  560.     if(!r) goto goodsec;
  561.    }
  562.   gotoxy(1,15);
  563.   printf("Bad sector (%02X): drive %02x, head %u, cyl %u, sector %u\n",
  564.          r,drive,head,cyl,sector+j);
  565.   goodsec:
  566.   buffer+=secsize;
  567.   }
  568. }
  569.